import Pino from "pino";

/** 日志配置项 */
type LoggerConfig = {
  /** 是否是生产环境, 生产环境: info且不格式化日志 */
  production?: boolean;
  /** 日志等级, 默认: 根据 production 确定 */
  level?: string;
  name?: string;
  prettry?: boolean;
};

export let logger: Pino.Logger;

/**
 * 初始化日志记录器
 * @param level 日志等级
 * @param label 日志标签
 * @returns
 */
export function init(config?: LoggerConfig) {
  if (logger) {
    return;
  }
  let c: LoggerConfig = { production: true, ...config };
  let level = c.level ?? (c.production ? "info" : "debug");
  let transport = undefined;
  if (c.prettry ?? !c.production) {
    transport = {
      target: "pino-pretty",
    };
  }
  logger = Pino({ level, name: c.name, transport });
}
